import { Controller } from 'egg';
import {OrderBy} from '../../service/db/ordered_schedule';

export default class AdminOrderedScheduleController extends Controller {
    public async getByTeacher() {
        const { ctx, service } = this;
        const page = ctx.params.page ? parseInt(ctx.params.page, 10) : 1;
        const teacherId = parseInt(ctx.params.id, 10);
        const result =
            await service.orderedSchedule.getByTeacherAndPage(teacherId, page, 10);
        ctx.helper.success(ctx, result);
    }

    public async getByStudent() {
        const { ctx, service } = this;
        const page = ctx.params.page ? parseInt(ctx.params.page, 10) : 1;
        const studentId = parseInt(ctx.params.id, 10);
        const result =
            await service.orderedSchedule.getByStudentAndPage(studentId, page, 10);
        ctx.helper.success(ctx, result);
    }

    public async getByDate() {
        const { ctx, service } = this;
        const { start_date, end_date, teacher_ids } = ctx.request.body;

        const result =
            await service.admin.orderedSchedule.getByDate(teacher_ids, start_date, end_date);
        ctx.helper.success(ctx, result);
    }

    public async addByTimes() {
        const { ctx, service } = this;
        ctx.validate({
            teacher_id: 'number',
            student_id: 'number',
            date: 'string',
            time_from: 'string',
            time_to: 'string',
            times: {
                type: 'number',
                min: 1,
                allowEmpty: false,
                required: true,
            },
        });
        const {
            teacher_id,
            student_id,
            date,
            time_from,
            time_to,
            times,
        } = ctx.request.body;
        await service.admin.orderedSchedule.addByTimes(teacher_id, student_id, date, time_from, time_to, times);
        ctx.helper.success(ctx);
    }

    public async deleteAll() {
        const { ctx, service } = this;

        ctx.validate({
            ordered_schedule_ids: {
                type: 'array',
                itemType: 'number',
            },
        });
        const orderedScheduleIds = ctx.request.body.ordered_schedule_ids || [];
        await service.admin.orderedSchedule.deleteAll(orderedScheduleIds);

        ctx.helper.success(ctx);
    }

    public async updateOne() {
        const { ctx, service } = this;
        ctx.validate({
            id: 'number',
            date: 'string',
            time_from: 'string',
            time_to: 'string',
        });

        await service.db.orderedSchedule.update(ctx.request.body);

        ctx.helper.success(ctx);
    }

    public async get() {
        const { ctx, service } = this;
        const page = ctx.params.page ? parseInt(ctx.params.page, 10) : 1;
        const orderBy = ctx.params.orderBy ? parseInt(ctx.params.orderBy, 10) : OrderBy.DATETIME_DESC;
        const {startDate, endDate, userId} = ctx.request.query;
        const result =
            await service.admin.orderedSchedule.getByPage(page, 50, orderBy, startDate, endDate, userId);
        ctx.helper.success(ctx, result);
    }
}
